<?php
// +----------------------------------------------------------------------
// | Bwsaas
// +----------------------------------------------------------------------
// | Copyright (c) 2015~2023 http://www.buwangyun.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Gitee ( https://gitee.com/buwangyun/bwsaas )
// +----------------------------------------------------------------------
// | Author: buwangyun <hnlg666@163.com>
// +----------------------------------------------------------------------
// | Date: 2023-06-25 10:55:00
// +----------------------------------------------------------------------

declare (strict_types = 1);

namespace buwang\command;


use buwang\util\console\CliEcho;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option as InputOption;
use think\console\Output;
use think\migration\command\seed\Run;

class SeedRunCommand extends Run
{
    //应用目录名
    protected $app_name;

    protected function configure()
    {
        // 指令配置
        $this->setName('mn-seed:run')
            ->setDescription('Run database seeders')
            ->addArgument('app_name', Argument::REQUIRED, 'seed the app database')
            ->addOption('--seed', '-s', InputOption::VALUE_REQUIRED, 'What is the name of the seeder?')
            ->setHelp('php think mn-seed:run app_name -s MySeeder');
    }

    protected function execute(Input $input, Output $output)
    {
        $this->app_name = strtolower($input->getArgument('app_name'));
        $seed = $input->getOption('seed');

        // run the seed command
        try {
            $start = microtime(true);
            $this->seed($seed);
            $end = microtime(true);
            $this->seeds = null;
            $output->writeln("Command executed successfully ". sprintf('%.4fs', $end - $start));
        } catch (\Exception $e) {
            $output->writeln("Command failed " . $e->getMessage());
        }
    }

    /**
     * 重新设置 migration path
     * @time 20230625
     * @return string
     */
    protected function getPath(): string
    {
        return app()->getAppPath() . $this->app_name . DS. 'database' . DS. 'seeds' . DS;
    }
}
